/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.manchester.biology.eqhei.session;


import edu.manchester.biology.eqhei.implementations.eQHEI_AttributeSetIMP;
import edu.manchester.biology.eqhei.interfaces.eQHEI_AttributeSet;
import edu.manchester.biology.eqhei.persistence.AttributeSet;
import edu.manchester.biology.eqhei.persistence.AttributeSetPK;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author mlwyatt
 */
@Stateless
@LocalBean
public class AttributeSetBean {

     private EntityManagerFactory    entityManagerFactory = Persistence.createEntityManagerFactory("Alspark-ejbPU");
    private EntityManager           entityManager;
    private eQHEI_AttributeSet attributeSetReturn = null;
    
    
    public eQHEI_AttributeSet getAttributeSet(String attributeSetName) {
        System.out.println("called getAttributeSet()");
        try
        {
            entityManager = entityManagerFactory.createEntityManager();
            if ( entityManager == null )
            {
                System.out.println("entity manager is null");
                return null;
            }
            AttributeSetPK pk=new AttributeSetPK();
            pk.setAttributeSetName(attributeSetName);
//            AttributeSet attributeSet = entityManager.find(AttributeSet.class, pk);
            AttributeSet attributeSet=new AttributeSet(attributeSetName,null);
            Query queryAttributeSet=entityManager.createNamedQuery("findByAttributeSetName");
            queryAttributeSet.setParameter("attributeSetName", attributeSetName);
            Collection<AttributeSet> attributeSets = queryAttributeSet.getResultList();
         
            if ( attributeSets == null )
            {
                System.out.println("returned null attribute sets");
                return null;
            }
            for(AttributeSet a:attributeSets){
                System.out.println(a.getAttributeSetPK().getAttributeSetName());
            }
            attributeSetReturn = new eQHEI_AttributeSetIMP(pk.getAttributeSetName());
            
        }
        catch(Exception e)
        {
            System.out.println("***** " + e.getMessage() + " *****");
            return null;
        }
        finally{
            entityManager.close();
            
        }
        
        
        return attributeSetReturn;
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public void createAttributeSet(eQHEI_AttributeSet attributeSet) {
    }
}
